﻿using System;
using System.Windows.Forms;

namespace DropboxImageStorage
{
  public partial class OAuthControl : UserControl
  {
    public event EventHandler SignIn;

    public event EventHandler CancelWaiting;

    public event EventHandler Unlink;

    public AuthState AuthState { get; private set; }

    public OAuthControl()
    {
      InitializeComponent();

      pnlSignIn.Top = 0;
      pnlSignIn.Left = 0;
      pnlSignIn.Dock = DockStyle.Fill;

      pnlWaitAuth.Top = 0;
      pnlWaitAuth.Left = 0;
      pnlWaitAuth.Dock = DockStyle.Fill;

      pnlUnlink.Top = 0;
      pnlUnlink.Left = 0;
      pnlUnlink.Dock = DockStyle.Fill;

      SetState(AuthState.NotAuthorized);
    }

    public void SetState(AuthState state)
    {
      pnlSignIn.Visible = state == AuthState.NotAuthorized;
      pnlWaitAuth.Visible = state == AuthState.WaitAuthorization;
      pnlUnlink.Visible = state == AuthState.Authorized;

      AuthState = state;
    }

    private void InvokeSignIn()
    {
      EventHandler handler = SignIn;
      if (handler != null)
        handler(this, EventArgs.Empty);
    }

    private void InvokeCancelWaiting()
    {
      EventHandler handler = CancelWaiting;
      if (handler != null)
        handler(this, EventArgs.Empty);
    }

    private void InvokeUnlink()
    {
      EventHandler handler = Unlink;
      if (handler != null)
        handler(this, EventArgs.Empty);
    }

    private void btnSignIn_Click(object sender, EventArgs e)
    {
      InvokeSignIn();
    }

    private void btnCancelAuthWaiting_Click(object sender, EventArgs e)
    {
      InvokeCancelWaiting();
    }

    private void btnUnlink_Click(object sender, EventArgs e)
    {
      InvokeUnlink();
    }
  }

  public enum AuthState
  {
    NotAuthorized,
    WaitAuthorization,
    Authorized
  }
}